Skip to content

fix(verify): reject stale blocks and registry read failures - #14

Merged
Canvinus merged 2 commits into
mainfrom
fix/verify-guards
Nov 28, 2026
Merged

Canvinus merged 2 commits into
mainfrom
fix/verify-guards

Conversation

@Canvinus

@Canvinus Canvinus commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Why

Since #11 pinned verification to one block, a request can name a historical blockId. If the code deployed at that block was verified earlier, the controller rebuilt it and called set_contract with the older hash and height. The registry's set_contract replaces the record without comparing heights, so the record for the code deployed now was replaced by an older one. After that the frontend shows a mismatch for a correctly verified contract, and get_contracts_by_code_hash no longer finds the current hash.

The controller couldn't guard against this because it never reliably knew whether a record existed. Root cause: VerifierService.getContract returned null for every error, and the controller swallowed errors as well, so "registry unreachable" and "no record" were the same value. During an RPC outage an already-verified contract got a full rebuild, an IPFS pin and a second signed write. get_contract returns Option, so a genuinely absent record is JSON null, which can be told apart from a failure.

The unchanged-hash check also ran only after the Docker build, although #11 already reads the deployed hash at the pinned block before building.

Change

  • VerifierService.getContract returns null only when get_contract answers JSON null. Its only caller is the verify controller. It throws HttpException(503) in these cases:

    • the RPC fails;
    • the RPC result isn't an array of bytes (0–255), for example a string, since Buffer.from would otherwise decode a text "null" as an absent record;
    • the decoded JSON is neither null nor an object;
    • the record lacks a non-empty string code_hash;
    • the record's block_height isn't a safe non-negative integer or a canonical decimal string, matching the strictness of the existing metadata/view_code checks.

    It returns block_height as a number.

  • POST /verify/rust:

    • A registry read failure or an unreadable record returns 503 Could not read the verifier registry for <account>. Please retry later. before metadata, clone, build, pin or write.
    • Once the pinned metadata and view_code reads are done (before cloning):
      • If the record's code_hash equals the deployed hash, it returns the existing 400 {"message": "Code hash didn't change"}.
      • If the pinned block height is below the record's block_height, it returns 409 Block <n> is older than the verified record for <account> at block <m>. Verify at block <m> or later. Equal and newer heights proceed as before.
    • The post-build unchanged-hash check stays as a backstop.
    • The registry is still read at finality: final, not at the pinned block, because the guards compare against the record as it is now.
  • nest/Dockerfile: cargo install near-cli-rs --version 0.31.1 --locked. The controller parses this CLI's output and relies on at-block-hash. 0.31.1 is what production runs and is the current crates.io stable (checked, not yanked).

  • The request shape and the success response are unchanged.

Known follow-up (out of scope): two jobs for the same account that finish in reverse order can still let the older one write last. A check-then-write in the backend can't close that race. It needs set_contract in the registry contract to reject a block_height below the stored one.

This PR changes input validation, code-hash comparisons and the conditions for registry writes, so it needs maintainer review under AGENTS.md.

Validation

Run in a fresh OrbStack VM (Ubuntu 24.04, Node 22.23.5, no credentials), from nest/, at 057f26f:

  • npm ci: OK
  • npm run lint: exit 0, and --fix left no changes to the committed files
  • npm test -- --runInBand: 12 suites, 113 tests passed
  • npm run build: OK
  • Red/green check on the 65 tests in verify.controller.spec.ts and verifier.service.spec.ts:
    • against main's verify.controller.ts and verifier.service.ts, 51 fail;
    • against the first PR commit 5c2327f, 28 fail (the review findings: a string result containing "null", missing or invalid code_hash, and block_height parsing in the service);
    • all pass at 057f26f.

What the tests cover:

  • Controller:
    • an older block is rejected with 409 and no clone, build, pin or write;
    • a registry read failure returns 503 with no metadata read, clone, build, pin or write;
    • an absent record (null) proceeds to 200, and the test checks the message and checksum body;
    • an unchanged hash returns the existing 400 with no clone or build;
    • equal and newer heights still verify.
  • Service:
    • 503 for an RPC error, a missing reply, a result that isn't a byte array (a string "null", a string record, an object, an array-like object, out-of-range or non-integer bytes), invalid JSON and non-object JSON;
    • 503 for a missing, empty or non-string code_hash;
    • 503 for a malformed block_height: missing, null, negative, fractional, 1e400, unsafe integers, "01", "1e3", " 1", "0x10", booleans, arrays and objects;
    • numeric and canonical-string heights are returned as numbers.

Not run: the production Docker image wasn't built, so the near-cli-rs pin is untested here. The staging replay is a separate later step.

🤖 Generated with Claude Code

Canvinus and others added 2 commits November 28, 2026 15:04
Since verification is pinned to one block (#11), a request can name a
historical blockId. The controller then rebuilt the code deployed at
that block and called set_contract with its older hash and height. The
registry's set_contract does not compare heights, so this replaced the
record for the code deployed now.

The controller could not guard against that because it never knew
whether a record existed: VerifierService.getContract returned null for
every error, and the controller swallowed errors too, so an RPC outage
looked like "no record" and led to a full rebuild, an IPFS pin and a
second signed write.

getContract now returns null only when get_contract answers JSON null
(it returns Option), and throws a 503 when the registry can't be read or
its answer isn't a record. The controller returns 503 with a retry
message before any other read or build.

With the record and the deployed hash at the pinned block known before
cloning, the controller now:
- returns the existing 400 "Code hash didn't change" when the record
  already holds the deployed hash, instead of after a Docker build;
- refuses (500) when the record's block_height is not a safe
  non-negative integer or a canonical decimal string;
- rejects (409) a pinned block below the record's block_height.

The post-build unchanged-hash check stays as a backstop.

nest/Dockerfile pins near-cli-rs to 0.31.1, the version production runs:
the controller parses this CLI's output and relies on at-block-hash.

Two jobs that finish in reverse order can still let the older one write
last; fixing that needs the registry contract to compare heights.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
getContract decoded the RPC result with Buffer.from, which also accepts
a string, so a malformed reply whose result was the text "null" decoded
to JSON null and counted as an absent record. That skipped both
pre-build guards and could lead to a clone, build, IPFS pin and write
where a retryable 503 is required. A decoded object without a string
code_hash was also returned as a record, so the unchanged-hash check
fell through to a build and write.

getContract now requires the result to be an array of bytes, and
requires a returned record to have a non-empty string code_hash and a
block_height that is a safe non-negative integer or a canonical decimal
string. It returns block_height as a number. Anything else throws the
same 503 as an RPC failure. The height parsing moved from the
controller into the service, so a malformed record height is now a 503
instead of a 500.

The absent-record controller test also asserts the success body.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Canvinus
Canvinus merged commit 787aa12 into main Nov 28, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant